package com.erp.programmer.controller;


import com.alibaba.fastjson.JSON;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.RoleMapper;
import com.erp.programmer.domain.Role;
import com.erp.programmer.dto.*;
import com.erp.programmer.service.ILocalCompanyService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.erp.programmer.utils.ValidateEntityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 本公司员工信息控制层
 *
 * @author 陈鑫
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/local_company")
@CrossOrigin
public class LocalCompanyController {
    @Autowired
    ILocalCompanyService iLocalCompanyService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RoleMapper roleMapper;

    private static final Logger logger = LoggerFactory.getLogger(LocalCompanyController.class);


    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    public ResponseDto<LoginUserDto> login(@RequestBody LocalCompanyDto localCompanyDto) {
        if (localCompanyDto == null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 空值验证
        if (CommonUtil.isEmpty(localCompanyDto.getUsername())) {
            return ResponseDto.errorByMsg(CodeMsg.USERNAME_EMPTY);
        } else if (CommonUtil.isEmpty(localCompanyDto.getPassword())) {
            return ResponseDto.errorByMsg(CodeMsg.PASSWORD_EMPTY);
        }
        return iLocalCompanyService.login(localCompanyDto);
    }

    /**
     * 获取所有用户信息
     */
    @PostMapping("/get_data")
    public ResponseDto<PageDto<LocalCompanyDto>> getAll(@RequestBody PageDto<LocalCompanyDto> pageDto) {
        return iLocalCompanyService.getAll(pageDto);
    }

    /**
     *保存员工信息
     */
    @PostMapping("/save")
    public ResponseDto<PageDto<LocalCompanyDto>> saveLocalCompany(@RequestBody LocalCompanyDto localCompanyDto) {
        if (localCompanyDto==null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(localCompanyDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }
        if(CommonUtil.isEmpty(localCompanyDto.getId())){
            //添加
            localCompanyDto.setId(null);
            return iLocalCompanyService.insertLocalCompany(localCompanyDto);
        }else{
            //修改
            return iLocalCompanyService.updateLocalCompany(localCompanyDto);
        }
    }

    /**
     * 检查用户是否登录
     * @return
     */
    @PostMapping("/user/check_login")
    public ResponseDto<LocalCompanyDto> checkLogin(@RequestBody LoginUserDto loginUserDto){
        if(loginUserDto == null || CommonUtil.isEmpty(loginUserDto.getToken())){
            return ResponseDto.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        ResponseDto<LocalCompanyDto> responseDto = getLoginUser(loginUserDto.getToken());
        if(responseDto.getCode() != 0){
            return responseDto;
        }
        logger.info("获取到的登录信息={}", responseDto.getData());
        return ResponseDto.success(responseDto.getData());
    }


    /**
     * 获取当前登录用户
     * @return
     */
    public ResponseDto<LocalCompanyDto> getLoginUser(String token){
        String value = stringRedisTemplate.opsForValue().get(token);
        if(CommonUtil.isEmpty(value)){
            return ResponseDto.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        LocalCompanyDto localCompanyDto = JSON.parseObject(value, LocalCompanyDto.class);
        Role role = roleMapper.selectById(localCompanyDto.getRoleId());
        RoleDto roleDto = CopyUtil.copy(role, RoleDto.class);
        localCompanyDto.setRoleDto(roleDto);
        return ResponseDto.success(localCompanyDto);
    }

    /**
     * 删除员工信息
     */
    @PostMapping("/delete")
    public ResponseDto<PageDto<LocalCompanyDto>> deleteLocalCompany(@RequestBody LocalCompanyDto localCompanyDto) {
        if (localCompanyDto==null || CommonUtil.isEmpty(localCompanyDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        if (!iLocalCompanyService.removeById(localCompanyDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.USER_DELETE_ERROR);
        }
        PageDto<LocalCompanyDto> pageDtoList = getAll(new PageDto<LocalCompanyDto>()).getData();
        ResponseDto<PageDto<LocalCompanyDto>> responseDto = iLocalCompanyService.getAll(pageDtoList);
        responseDto.setMsg("删除成功！");
        return responseDto;
    }

}

